package exhi.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.stp.core.util.Identities;
import com.stp.std.entity.AuditLog;

import exhi.entity.EntpBexhi;
import exhi.entity.EntpSexhi;
import exhi.manager.EntpBexhiMng;
import exhi.manager.EntpSexhiMng;

@Controller
@RequestMapping("/entp")
public class EntpPrintAct extends ViewExhiAct {
	// 企业打印申请表
	private static final String PATH_EXHI = "/print-corp-exhi";

	// 企业打印申请表
	private static final String PATH_SEXHI = "/print-corp-sexhi";

	// 企业打印承诺书
	private static final String PATH_CHENGNUO = "/print-corp-chengnuo";

	// 企业打印申请表
	@RequestMapping(PATH_EXHI)
	public void infoView(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpBexhiInfo);
		if ("30".equalsIgnoreCase(entpBexhiInfo.getStatus())){
			AuditLog audit = new AuditLog();
			audit.setAuditGroup("S");
			audit.setAuditId(entpBexhiInfo.getId());
			audit.setAuditSendId("40");
			audit.setAuditTitle("已打印报批");
			audit.setAuditTime(new Date());
			if (null != getEntpUser())
				audit.setAuditName(getEntpUser().getMc());
			audit.setAuditStatus("T");
			auditLogMng.save(audit);
			entpBexhiInfo.setStatus("40");
			entpBexhiMng.update(entpBexhiInfo);
		}	
		showBexhiView(entpBexhiInfo, model);
	}

	// 企业打印申请表
	@RequestMapping(PATH_SEXHI)
	public void infoView(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpSexhi") EntpSexhi entpSexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpSexhiInfo);
		
		if ("30".equalsIgnoreCase(entpSexhiInfo.getStatus())){
			AuditLog audit = new AuditLog();
			audit.setAuditGroup("S");
			audit.setAuditId(entpSexhiInfo.getId());
			audit.setAuditSendId("40");
			audit.setAuditTitle("已打印报批");
			audit.setAuditTime(new Date());
			if (null != getEntpUser())
				audit.setAuditName(getEntpUser().getMc());
			audit.setAuditStatus("T");
			auditLogMng.save(audit);
			entpSexhiInfo.setStatus("40");
			entpSexhiMng.update(entpSexhiInfo);
		}		
		showSexhiView(entpSexhiInfo, model);
	}

	// 企业打印申请表
	@RequestMapping(PATH_CHENGNUO)
	public void chengnuoView(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpBexhiInfo);
		showBexhiView(entpBexhiInfo, model);
	}

		
	@ModelAttribute("entpSexhi")
	public EntpSexhi getEntpSexhiInfo(
			@RequestParam(value = "sinfoId", required = true, defaultValue = "") String id) {
		EntpSexhi entpSexhi = entpSexhiMng.get(id);
		if (null == entpSexhi) {
			entpSexhi = new EntpSexhi();
		}
		return entpSexhi;
	}
	
	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpBexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}


	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;
	
	@Autowired
	@Qualifier("entpSexhiMng")
	private EntpSexhiMng entpSexhiMng;
}
